var sys = {};

sys.vue = new Vue();
/**
 * 自定义的AJAX封装
 * @param {String} url
 * @param {String|Array|Object|HTMLFormElement|FormData|URLSearchParams} data
 * @param {JSONObject} config
 */
sys.ajax = function(url, data, config={}) {
  if (data != null) {
    if(config.method=="get"){
       config.body=null;
       if(url.indexOf("?")>-1){
         url += "&"+sys.params(data).toString();
       }else{
         url += "?"+sys.params(data).toString();
       }
    }else{
      config.body=sys.params(data).toString();
    }
  }
  config = Object.assign({
    method:"post",
    cache: 'no-cache',
    credentials:"include",
    headers:{"content-type":"application/x-www-form-urlencoded"},
   }, config);
   return new Promise((success,error)=>{
     fetch(url,config).then((r)=>{
       sys.loading.hide();
       if(r.status==200){
         return r.json();
       }else{
         return Promise.reject(new Error("请求错误:"+r.status));
       }
       
     }).then((data)=>{
       if(data.hasOwnProperty("ok")){
         if(data.ok){
           success(data);
         }else{
           sys.notify(data.message,"错误提示","error");
           error(data);
         }
       }else{
         success(data);
       }
     }).catch((e)=>{
       console.warn(e);
       sys.loading.hide();
       sys.message(e.message==null?"网络错误":e.message,"error");
     });
   });
}

/**
 * 格式化参数
 * @param {String|Array|Object|HTMLFormElement|FormData|URLSearchParams} param
 * @return {URLSearchParams}
 */
sys.params = function(param) {
  if (param == null || param == "") {
    return null;
  }
  if (param.constructor == Array) {
    let param1 = new URLSearchParams();
    for (let obj of param) {
      param1.append(obj.name, obj.value);
    }
    param = param1;
  } else if (param.constructor == Object) {
    let param1 = new URLSearchParams();
    for (let name in param) {
      param1.append(name, param[name]);
    }
    param = param1;
  } else {
    if (param.constructor == HTMLFormElement) {
      param = new FormData(param);
    }
    if (param.constructor == FormData || param.constructor == String) {
      param = new URLSearchParams(param);
    }
  }

  return param;
}

/**
 * 设置地址栏的某个参数的值
 * @param {Object} value
 * @param {Object} name 默认：page
 */
sys.setLocationParam = function(value,name="page"){
  let url = new URL(location.href);
  url.searchParams.set(name,value);
  location=url.toString();
}

/**
 * 显示消息
 * @param {String} msg 消息内容
 * @param {String} type 消息类型 默认：“success”
 */
sys.message = function(msg,type="success"){
  sys.vue.$message({message:msg,type:type});
}

/**
 * 显示通知
 * @param {String} msg 通知内容
 * @param {String} title 通知标题 默认："系统提示" 
 * @param {String} type 通知类型 默认：“success”
 */
sys.notify = function(msg,title="系统提示",type="success"){
  sys.vue.$notify({
          title: title,
          message: msg,
          type: type,
          position: 'bottom-right'
        });
}

/**
 * 加载中
 */
sys.loading = {}

/**
 * 显示加载中
 */
sys.loading.show = function(){
  if(sys.loading.obj!=null){
    return;
  }
  sys.loading.obj = sys.vue.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.1)'
        });
}

/**
 * 隐藏加载中
 */
sys.loading.hide = function(){
  if(sys.loading.obj!=null){
    sys.loading.obj.close();
    sys.loading.obj=null;
  }
}